# Copyright (C) 2006,2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#

use strict; use warnings;

package CCNCore::Bootstrap;
use base 'Jifty::Bootstrap';
use CCN::Text;

use CCNCore::bootstrap_utils;

sub run {
    my $user = CCNCore::CurrentUser->new( _bootstrap => 1 );

    my $env = CCNCore::bootstrap_utils::identify_node;
    # identify_node runs generate_keypair
    my $ssh_pubkey = CCN::Text::text_to_hex(content_of("$ENV{HOME}/.ssh/id_rsa.pub"));

    my $owner_class = CCNCore::Model::Groups->new( current_user => $user ) or die;
    $owner_class->create(
        name    => 'The Root Group',
        parent  => undef,
    );
    my $owner_id = $owner_class->id;

    my $systems_class = CCNCore::Model::Systems->new( current_user => $user ) or die;
    $systems_class->create( 
        dns_name    => 'carrierclass.net.voice', 
        fancy_name  => 'System', 
        owner       => $owner_id,
        salt        => rand(),
        timezone    => 'UTC', 
    );
    my $system_id = $systems_class->id;

    my $server_class = CCNCore::Model::Server->new( current_user => $user ) or die;
    $server_class->create(
        fancy_name          => 'Server',
        owner               => $owner_id,
        the_system          => $system_id,
        operating_system    => $env->{os},
        default_router      => $env->{default_router},
        dns_master          => 'TRUE',
        ntp_master          => 'TRUE',
    );
    my $server_id = $server_class->id;

    $systems_class->_set(
        column  => 'manager',
        value   => $server_id
    );
    $server_class->_set(
        column  => 'cdn_downstream_server',
        value   => $server_id
    );

    my $cluster_class = CCNCore::Model::DatabaseCluster->new( current_user => $user ) or die;
    $cluster_class->create( 
        fancy_name          => 'Origin Database',
        owner               => $owner_id, 
        the_system          => $system_id,
    );
    my $dbcl_id = $cluster_class->id;

    my $provisioning_class = CCNCore::Model::Provisioning->new( current_user => $user ) or die;
    $provisioning_class->create( 
        db_cluster          => $dbcl_id, 
        db_name             => 'carrierclass',
        the_system          => $system_id,
        owner               => $owner_id,
    );
    my $provisioning_id = $provisioning_class->id;

    my $provisioning_servers_class = CCNCore::Model::ProvisioningServers->new( current_user => $user ) or die;
    $provisioning_servers_class->create( 
        provisioning_system => $provisioning_id, 
        server              => $server_id, 
        ssh_pubkey          => $ssh_pubkey 
    );

    add_interfaces($server_id,$env,$user);
}

sub add_interfaces($$$)
{
    my $server_id = shift;
    my $env = shift;
    my $user = shift;

    for my $v (values %{$env->{interfaces}})
    {
        my $server_interface_class = CCNCore::Model::ServerInterface->new( current_user => $user );
        if( exists $v->{ipnet_ip} )
        {
            $server_interface_class->create(
                fancy_name  => "$v->{ifname}: $v->{mac}",
                mac         => $v->{mac},
                server      => $server_id,
                rank        => $v->{rank},
                ifname      => $v->{ifname},
                ipnet_ip    => $v->{ipnet_ip},
                ipnet_mask  => $v->{ipnet_mask},
                
            );
        }
        else
        {
            $server_interface_class->create(
                fancy_name  => "$v->{ifname}: $v->{mac}",
                mac         => $v->{mac},
                server      => $server_id,
                rank        => $v->{rank},
                ifname      => $v->{ifname},
                
            );
        }
    }
}


sub _parse_rpc($)
{
    my $fh = shift;
    my $env = {};

    while(<$fh>)
    {
        chomp;
        $env->{$1}=$2,             next if /^(\w+)=(.*)$/;
        $env->{$1}->{$2}=$3,       next if /^(\w+)\[(\w+)\]=(.*)$/;
        $env->{$1}->{$2}->{$3}=$4, next if /^(\w+)\[(\w+)\]\[(\w+)\]=(.*)$/;
    }

    return $env;
}

=pod
    remote_rpc
=cut

sub remote_rpc($$$$)
{
    my $fh = shift;
    my ($system_id,$owner_id,$cdn_server) = @_;

    my $env = _parse_rpc($fh);

    my $user = CCNCore::CurrentUser->superuser;

    my $server_class = CCNCore::Model::Server->new( current_user => $user );
    $server_class->create(
        fancy_name          => 'Server',
        owner               => $owner_id,
        the_system          => $system_id,
        operating_system    => $env->{os},
        default_router      => $env->{default_router},
        dns_master          => 'TRUE',
        ntp_master          => 'TRUE',
        cdn_downstream_server => $cdn_server,
    );
    my $server_id = $server_class->id;

    add_interfaces($server_id,$env,$user);

    return $server_id;
}

1;